package cn.wxxyhw.hander;

import cn.wxxyhw.dao.DepartmentDao;
import cn.wxxyhw.dao.EmployeeDao;
import cn.wxxyhw.entity.Department;
import cn.wxxyhw.entity.Employee;
import com.google.gson.Gson;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.ServletContext;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.io.File;
import java.io.InputStream;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

@SessionAttributes("employee")
@Controller("hello")
public class MyHander {
    @Autowired
    private EmployeeDao employeeDao;
    public final String SUCCESS="success";

   @ModelAttribute
    public void getEmpory(Employee employee,Map<String,Employee> map){
        employee = employeeDao.getEmployeeById("1");
        map.put("user",employee);
    }

    @Autowired
    private  EmployeeDao employees;
    @Autowired
    private DepartmentDao departmentDao;

    @RequestMapping("/emps")
    public String list(Map<String ,Object> map){
        map.put("all",employees.getEmployees());
        return "list";
    }

    @RequestMapping(value = "emp/{id}",method = RequestMethod.DELETE)
    public String delete(@PathVariable("id") Integer id){
        employeeDao.delete(String.valueOf(id));
        return "redirect:/emps";
    }

    @RequestMapping(value = "emp",method = RequestMethod.POST)
    public String save(Employee employee){
        employeeDao.save(employee);
        return "redirect:/emps";
    }

    @RequestMapping(value="emp",method = RequestMethod.GET)
    public String add(Map<String, Object> map){
        map.put("department",departmentDao.getAll());
        map.put("employee",new Employee());
        Map<String,String> gmap = new HashMap<>();
        gmap.put("男","男");
        gmap.put("女","女");
        map.put("gender",gmap);
        return "input";
    }

    @ResponseBody
    @RequestMapping("httpMessageConvter")
    public String httpMessageConvter(@RequestBody String content){
        System.out.println(content);
        return "httpMessageConvter "+new Date();
    }

    /*文件的下载*/
    @RequestMapping("filexiazai")
    public ResponseEntity<byte[]> filexiazai(HttpSession session) throws Exception{
        byte[] body = null;
        ServletContext servletContext = session.getServletContext();
        InputStream resourceAsStream = servletContext.getResourceAsStream("/file/layuiAdmin.std-v1.2.1.rar");
        body= new byte[resourceAsStream.available()];
        resourceAsStream.read(body);
        HttpHeaders headers = new HttpHeaders();
        headers.add("Content-Disposition","attachment;filename=layuiAdmin.std-v1.2.1.rar");

        ResponseEntity<byte[]> responseEntity = new ResponseEntity<byte[]>(body,headers, HttpStatus.CREATED);
        return responseEntity;
    }

    /*文件的上传*/
    @RequestMapping("fileupload")
    public String fileupload(HttpServletRequest request,
                             @RequestParam("desc") String desc,
                             @RequestParam("file")MultipartFile file) throws Exception{
        String path =request.getServletContext().getRealPath("/file/");
        System.out.println(path);
        file.transferTo(new File(path+File.separator+file.getOriginalFilename()));
        return SUCCESS;
    }

    @RequestMapping("/test/{id}")
    public String test(@PathVariable int id){
        System.out.println(id);
        return SUCCESS;
    }

    @RequestMapping("xiugai")
    public String xigai(Employee employee){
        System.out.println(employee);
        return SUCCESS;
    }

    @RequestMapping("/myview")
    public String myview(){
        return "helloView";
    }

    @RequestMapping("/conversion")
    public String conversion(@RequestParam("user") Employee employee){
        System.out.println(employee);
        return SUCCESS;
    }
}
